const db = require('../db/index')
const moment = require('moment')



// 获取产品的类别和总价
exports.getCateroryAndTotal = (req, res) => {

  // 获取产品类别数组
  const getCateArr = () => {
    return new Promise(resolve => {
      const sql = 'select set_value from setting where set_name = "产品设置"'
      db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        resolve(JSON.parse(results[0].set_value))
      })
    })
  }

  // 获取产品总价数组
  const getTotalArr = (product_cate) => {
    return new Promise(resolve => {
      const sql = 'select product_all_price from product where product_category = ?'
      db.query(sql, product_cate, (err, results) => {
        if (err) return res.cc(err)
        let total = 0
        for (let i = 0; i < results.length; i++) {
          total += results[i].product_all_price
        }
        resolve(total)
      })
    })
  }

  // 获取产品类别和总价
  const getAll = async () => {
    const cateArr = await getCateArr()
    const priceArr = []
    for (let i = 0; i < cateArr.length; i++) {
      priceArr[i] = await getTotalArr(cateArr[i])
    }
    res.send({
      status: 0,
      message: '获取产品类别和总价成功',
      data: {
        category: cateArr,
        price: priceArr
      }
    })
  }
  getAll()
}

// 获取不同角色的用户数量
exports.getIdentityCount = (req, res) => {

  // 获取某个身份总数量
  const getCount = (identity) => {
    return new Promise(resolve => {
      const sql = `select count(*) as total from users where identity = ?`
      db.query(sql, identity, (err, results) => {
        resolve(results[0].total)
      })
    })
  }

  // 获取不同角色的用户数量, 返回对应角色的数量的数组
  const getAll = async () => {
    const data = [{
      value: 0,
      name: '超级管理员'
    },
    {
      value: 0,
      name: '产品管理员'
    },
    {
      value: 0,
      name: '用户管理员'
    },
    {
      value: 0,
      name: '消息管理员'
    },
    {
      value: 0,
      name: '用户'
    }
    ]

    for (let i = 0; i < data.length; i++) {
      data[i].value = await getCount(data[i].name)
    }

    res.send({
      status: 0,
      message: '获取不同角色的用户数量成功',
      data: data
    })
  }

  getAll()
}


// 获取不同等级的消息数量
exports.getLevelCount = (req, res) => {

  // 获取某个等级总数量
  const getCount = (level) => {
    return new Promise(resolve => {
      const sql = `select count(*) as total from message where message_level = ?`
      db.query(sql, level, (err, results) => {
        resolve(results[0].total)
      })
    })
  }

  // 获取不同等级的消息数量, 返回对应等级的数量的数组
  const getAll = async () => {
    const data = [{
      value: 0,
      name: '一般'
    },
    {
      value: 0,
      name: '重要'
    },
    {
      value: 0,
      name: '必要'
    },
    ]

    for (let i = 0; i < data.length; i++) {
      data[i].value = await getCount(data[i].name)
    }

    res.send({
      status: 0,
      message: '获取不同等级的消息数量',
      data: data
    })
  }

  getAll()
}

// 获取最近七天的的登录人数
exports.getLoginCountonWeek = (req, res) => {

  // 获取最近七天的日期
  const getDay = () => {
    let date = new Date()
    let dayArr = []
    // for (let i = 0; i < 7; i++) {
    //   date.setDate(date.getDate() - 1)
    //   dayArr[i] = moment(date).format('YYYY-MM-DD')
    // }
    for (let i = 0; i < 7; i++) {
      dayArr.unshift(moment(date).subtract(i, 'days').format('YYYY-MM-DD')) // 使用unshift在数组开头添加元素
    }
    return dayArr
  }

  // 获取每天的登录人数
  const getCount = (login_time) => {
    return new Promise(resolve => {
      const sql = `select count(*) as total from  login_log where login_time like '%${login_time}%'`
      db.query(sql, login_time, (err, results) => {
        if (err) return res.cc(err)
        resolve(results[0].total)
      })
    })
  }

  const getAll = async () => {
    const dayArr = getDay()
    const countArr = []

    for (let i = 0; i < dayArr.length; i++) {
      countArr[i] = await getCount(dayArr[i])
    }

    const data = {
      day: dayArr,
      count: countArr
    }

    res.send({
      status: 0,
      message: '获取最近七天的的登录人数',
      data
    })
  }

  getAll()
}